﻿
using System.Runtime.InteropServices;
using System.Text.Json;
using Sirius.SystemDll;

namespace Sirius.Application
{
    public class ApplicationMethodBase
    {
        //应用信息
        public delegate IntPtr AppInfo(string DataPath);
        
        //频道"@"消息
        public delegate int _EventGuildAtMessage(string data);

        //应用菜单A
        public delegate void MenuA();

        //初始化机器人信息
        public delegate bool Bot_Init(string data);

        //频道事件
        public delegate int _eventGuild(string instance, string data);

        //子频道事件
        public delegate int _eventChannel(string instance, string data);

        //频道消息事件_仅限私域可用
        public delegate int _eventGuildMessage(string instance, string data);

        //成员私信事件
        public delegate int _eventPrivateMessage(string data);

        //频道成员事件
        public delegate int _eventGuildMember(string instance, string data);

        //表情表态事件
        public delegate int _eventMessageReaction(string instance, string data);

        //互动消息事件(通常为按钮事件)
        public delegate int _eventInteractionMessage(string data, string event_id, string msg_id, string button_id, string button_data, string user_id, string channel_id, string guild_id);
    }
    public class ApplicationMethod
    {
        public readonly ApplicationMethodBase.AppInfo app_info;

        public readonly ApplicationMethodBase._EventGuildAtMessage GuildAtMessage;

        public readonly ApplicationMethodBase.MenuA _menuA;

        public readonly ApplicationMethodBase.Bot_Init botInit;

        public readonly ApplicationMethodBase._eventGuild eventGuild;

        public readonly ApplicationMethodBase._eventChannel eventChannel;

        public readonly ApplicationMethodBase._eventGuildMessage eventGuildMessage;

        public readonly ApplicationMethodBase._eventPrivateMessage eventPrivateMessage;

        public readonly ApplicationMethodBase._eventGuildMember eventGuildMember;

        public readonly ApplicationMethodBase._eventMessageReaction eventMessageReaction;

        public readonly ApplicationMethodBase._eventInteractionMessage eventInteractionMessage;
        //禁止创建对象
        private ApplicationMethod(IntPtr instance)
        {
            //应用实例化

            //定义空指针函数，用于判断函数是否为空
            var NullPointer = IntPtr.Zero;

            var appInfo = kernel32.GetProcAddress(instance, "AppInfo");
            if (appInfo != NullPointer)
                app_info = Marshal.GetDelegateForFunctionPointer<ApplicationMethodBase.AppInfo>(appInfo);

            var menuA = kernel32.GetProcAddress(instance, "_menuA");
            if(menuA != NullPointer)
                _menuA = Marshal.GetDelegateForFunctionPointer<ApplicationMethodBase.MenuA>(menuA);

            var guildAtMessage = kernel32.GetProcAddress(instance, "_eventGuildAtMessage");
            if (guildAtMessage != NullPointer)
                GuildAtMessage = Marshal.GetDelegateForFunctionPointer<ApplicationMethodBase._EventGuildAtMessage>(guildAtMessage);

            var bot_Init = kernel32.GetProcAddress(instance, "InitBot");
            if (bot_Init != NullPointer)
                botInit = Marshal.GetDelegateForFunctionPointer<ApplicationMethodBase.Bot_Init>(bot_Init);

            var event_Guild = kernel32.GetProcAddress(instance, "_eventGuild");
            if(event_Guild!=NullPointer)
               eventGuild = Marshal.GetDelegateForFunctionPointer<ApplicationMethodBase._eventGuild>(event_Guild);

            var event_Channel = kernel32.GetProcAddress(instance, "_eventChannel");
            if(event_Channel!=NullPointer)
                eventChannel = Marshal.GetDelegateForFunctionPointer<ApplicationMethodBase._eventChannel>(event_Channel);

            var guild_Message = kernel32.GetProcAddress(instance, "_eventGuildMessage");
            if(guild_Message != NullPointer)
                eventGuildMessage = Marshal.GetDelegateForFunctionPointer<ApplicationMethodBase._eventGuildMessage>(guild_Message);

            var private_Message = kernel32.GetProcAddress(instance, "_eventPrivateMessage");
            if(private_Message!=NullPointer)
                eventPrivateMessage = Marshal.GetDelegateForFunctionPointer<ApplicationMethodBase._eventPrivateMessage>(private_Message);

            var event_Member = kernel32.GetProcAddress(instance, "_eventGuildMember");
            if(event_Member!=NullPointer)
                eventGuildMember = Marshal.GetDelegateForFunctionPointer<ApplicationMethodBase._eventGuildMember>(event_Member);

            var event_MessageReaction = kernel32.GetProcAddress(instance, "_eventMessageReaction");
            if (event_MessageReaction != NullPointer)
                eventMessageReaction = Marshal.GetDelegateForFunctionPointer<ApplicationMethodBase._eventMessageReaction>(event_MessageReaction);

            var event_Interaction = kernel32.GetProcAddress(instance, "_eventInteractionMessage");
            if (event_Interaction != NullPointer)
                eventInteractionMessage = Marshal.GetDelegateForFunctionPointer<ApplicationMethodBase._eventInteractionMessage>(event_Interaction);
        }   

        public static ApplicationMethod CreateMethod(IntPtr instance)
        {
            return new(instance);
        }

        public ApplicationInfo? getAppInfo(string DataPath)
        {
            if (app_info == null)
                return null;
            var data = app_info(DataPath);

            return JsonSerializer.Deserialize<ApplicationInfo>(Marshal.PtrToStringAnsi(data));
        }

    }
}
